<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const list = [
        {
          id: 1,
          pid: null,
          name: '中国'
        },
        {
          id: 2,
          pid: 1,
          name: '江西省'
        },
        {
          id: 3,
          pid: 1,
          name: '广东省'
        },
        {
          id: 4,
          pid: 1,
          name: '湖南省'
        },
        {
          id: 5,
          pid: 2,
          name: '南昌'
        },
        {
          id: 6,
          pid: 2,
          name: '赣州'
        },
        {
          id: 7,
          pid: 3,
          name: '深圳'
        },
        {
          id: 8,
          pid: 3,
          name: '佛山'
        },
        {
          id: 9,
          pid: 3,
          name: '珠海'
        },
        {
          id: 10,
          pid: 3,
          name: '汕头'
        },
        {
          id: 11,
          pid: 3,
          name: '广州'
        },
        {
          id: 12,
          pid: 4,
          name: '长沙'
        },
        {
          id: 13,
          pid: 4,
          name: '湘潭'
        }
      ]

      function treeing(arr) {
        let obj = {}
        let tree = []
        for (const item of arr) {
          let newItem = (obj[item.id] = {
            ...item,
            children: []
          })
          if (item.pid) {
            let parent = obj[item.pid]
            parent.children.push(newItem)
          } else {
            tree.push(newItem)
          }
        }
        return tree
      }

      const newListTree = treeing(list)
      console.log('[ newListTree ]-101', newListTree)

      function flatten(tree, arr = []) {
        tree.forEach(item => {
          let { children, ...props } = item
          arr.push(props)
          if (children) {
            flatten(item.children, arr)
          }
        })
        return arr
      }
      let newListTree2 = flatten(newListTree)
      console.log('[newListTree2  ] >', newListTree2)

      const m = new Map()
      // o是一个对象
      const o = { p: 'Hello world!' }
      // 添加成员：对象o是m的一个键，值为'content'
      m.set(o, 'content') // "content"
      console.log('[ m ]-118', m)

      const m2 = new Map([[{ p: 'Hello world!' }, 'content']])
      console.log(m2)
      for (let key of m.values()) {
        console.log(key)
        console.log('[ m.get(key) ] >', m.get(key))
      }
    </script>
  </body>
</html>
